% Turns an array of results into a nice LaTeX table.
% This version also print error bars.
%
% Adapted from Ryan Turner's code.
%
% David Duvenaud
% Feb 2012
% ========================
function final_results_table(filename, results, methodNames, metricNames, experimentName)

results = real(results);

file = fopen( filename, 'w');

for i = 1:length(methodNames)
    methodNames{i} = strrep(methodNames{i}, '_', ' ');
end

for i = 1:length(metricNames)
    metricNames{i} = strrep(metricNames{i}, '_', ' ');
end

% This is maximum digits left of the dot AFTER shifting the column over by
% the exponent from fixMatrix(). Note that MaxLeftDigits is not the same as
% maxSigFigs.
maxLeftDigits = 3;
maxClip = 10 ^ maxLeftDigits;

[methods metrics] = size(results);
assert(length(methodNames) == methods);
assert(length(metricNames) == metrics);
maxClipCol = zeros(metrics, 1);

% argmin might have trouble if methods is singleton. TODO fix
results = [results; nan(1, metrics)];
[best, best_ix] = min(results);
% nearbest keeps track of if we are significantly different from the best
% model.
nearbest = zeros( size(results));
for r = 1:methods
    for c = 1:metrics-1
        % run a paired ttest
        %if abs(meanScore(ii,jj) - best(jj)) <= stds(ii,jj) + stds(best_ix(jj), jj)
        %h = ttest(results(ii,jj,:), results(best_ix(jj), jj, :));
        if best_ix(c) == r %isnan(h) || h == 0
            nearbest(r,c) = 1;
        end
    end
end
% Crop the error bars to two digits, shift everything to right exponent, and
% crop the scores to match the error bars.
%[meanScore, errorBar, exponent, prec] = fixMatrix(meanScore, errorBar);

% Print all the usual table header stuff
fprintf(file, '%% --- Automatically generated by latex_table.m ---\n');
fprintf(file, '%% Exported at %s\n', datestr(now()));
fprintf(file, '\\begin{table}[h!]\n');
fprintf(file, '\\caption{{\\small\n');
fprintf(file, '%s\n', experimentName);
fprintf(file, '}}\n');
fprintf(file, '\\label{tbl:%s}\n', experimentName);
fprintf(file, '\\begin{center}\n');
fprintf(file, '\\begin{tabular}{l %s}\n', repmat(' r', 1, metrics));

% first line
fprintf(file, 'Method');
for c = 1:metrics
  fprintf(file, ' & \\rotatebox{0}{ %s } ', metricNames{c});
end

for c = 1:metrics
  % We don't want the clip to be so small even the best method gets clipped
  orderMagBest = exp10(2+ceil(log10(max(min(results(:, c)'), 0))));
  maxClipCol(c) = max(maxClip, orderMagBest);
end
%fprintf(file, ' \\\\ \\hline\n');
fprintf(file, ' \\\\ \\midrule\n');   % Using booktabs


% for each method
for r = 1:methods
  fprintf(file, methodNames{r});
  for c = 1:metrics
    printFormat = ['%4.3f'];
    
    %if best(jj) == ii
    if r == 2 && ( c == 1 || c == 3 )
      fprintf(file, ' & N/A');
    elseif nearbest(r, c)
      %fprintf(file, [' & $\\mathbf{' printFormat '} \\pm %2.1f$'], ...
      fprintf(file, [' & $\\mathbf{' printFormat '}$'], ...
        results(r, c));
    elseif results(r, c) > maxClipCol(c)
      fprintf(file, ' & $>$ %d', maxClipCol(c));
    else
      %fprintf(file, [' & $' printFormat ' \\pm %2.1f$' ], ...
      fprintf(file, [' & $' printFormat '$' ], ...
        results(r, c));
    end
  end
  fprintf(file, ' \\\\\n');
end

fprintf(file, '\\end{tabular}\n');
fprintf(file, '\\end{center}\n');
fprintf(file, '\\end{table}\n');
fprintf(file, '%% End automatically generated LaTeX\n');
fclose(file);

% Print something that you can cut and paste into latex
%fprintf('\\input{%s}', filename );
